Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: generic Header and Proof #1145

Merged
merged 16 commits into from
Sep 20, 2023
Merged

refactor: generic Header and Proof #1145

merged 16 commits into from
Sep 20, 2023

Conversation

lumtis
Copy link
Member

@lumtis lumtis commented Sep 19, 2023

Description

Make header and proof structures generic for adding new chain structures in the future.

A new header type can be added with the following:

  • Adding a new field in common.proto:HeaderData
  • Adding a new initializer such as
func NewEthereumProof(proof ethereum.Proof) *Proof {
	return &Proof{
		Proof: &Proof_EthereumProof{
			EthereumProof: &proof,
		},
	}
}
  • Adding ParentHash implementation in headers.go
func (h HeaderData) ParentHash() ([]byte, error) {
	switch data := h.Data.(type) {
	case *HeaderData_EthereumHeader:
		var header ethtypes.Header
		if err := rlp.DecodeBytes(data.EthereumHeader, &header); err != nil {
			return nil, err
		}
		return header.ParentHash.Bytes(), nil
	default:
		return nil, errors.New("unrecognized header type")
	}
}
  • Adding Validate implementation in headers.go
func (h HeaderData) Validate(blockHash []byte, height int64) error {
	switch data := h.Data.(type) {
	case *HeaderData_EthereumHeader:
		return validateEthereumHeader(data.EthereumHeader, blockHash, height)
	default:
		return errors.New("unrecognized header type")
	}
}

A new proof type can be added with the following

  • Adding a new field in common.proto:Proof
  • Adding a new initializer such as
func NewEthereumProof(proof ethereum.Proof) *Proof {
	return &Proof{
		Proof: &Proof_EthereumProof{
			EthereumProof: &proof,
		},
	}
}
  • Adding a new Verify implementation in proof.go
func (p Proof) Verify(headerData HeaderData, txIndex int) ([]byte, error) {
	switch proof := p.Proof.(type) {
	case *Proof_EthereumProof:
		ethHeaderBytes := headerData.GetEthereumHeader()
		if ethHeaderBytes == nil {
			return nil, errors.New("can't verify ethereum proof against non-ethereum header")
		}
		var ethHeader ethtypes.Header
		err := rlp.DecodeBytes(ethHeaderBytes, &ethHeader)
		if err != nil {
			return nil, err
		}
		val, err := proof.EthereumProof.Verify(ethHeader.TxHash, txIndex)
		if err != nil {
			return nil, NewErrInvalidProof(err)
		}
		return val, nil
	default:
		return nil, errors.New("unrecognized proof type")
	}
}

Closes: #1138

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Include instructions and any relevant details so others can reproduce.

  • Tested CCTX in localnet
  • Tested in development environment
  • Go unit tests
  • Go integration tests
  • Tested via GitHub Actions

Checklist:

  • I have added unit tests that prove my fix feature works

@lumtis lumtis self-assigned this Sep 19, 2023
@lumtis lumtis marked this pull request as ready for review September 19, 2023 14:47
common/proof.go Outdated Show resolved Hide resolved
Copy link
Contributor

@ws4charlie ws4charlie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good. Nothing blocking.

@lumtis lumtis merged commit b896e3c into develop Sep 20, 2023
18 of 19 checks passed
@lumtis lumtis deleted the refactor/header-generic branch September 20, 2023 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Define generic structures for block header and proof
3 participants